#!/bin/bash -eu
# Testing that PKG_CHECK_MODULES works.
# Note: this script must not be added to the test suite as it will change configure options.

echo "***************************************************************************************"

# 1. Define temporary directory.

export TMP_DIR="/tmp/tstAutotoolsInstall"
rm -fr "${TMP_DIR}" # Make sure we restart from scratch.
mkdir -p "${TMP_DIR}"

# 2. Rerun autotools with prefix, install arpack-ng.
#    Note: this script must not be added to the test suite as it will change configure options.

make clean
make distclean

export PROJECT_SOURCE_DIR="$(pwd)"
export TMP_PREFIX="${TMP_DIR}/local"
export LIBSUFFIX="${1%-*}"
echo "LIBSUFFIX: $LIBSUFFIX"
export ITF64SUFFIX="${1#*-}"
echo "ITF64SUFFIX: $ITF64SUFFIX"
LIBSUFFIX="${LIBSUFFIX}" ITF64SUFFIX="${ITF64SUFFIX}" ./configure --prefix="${TMP_PREFIX}" --enable-mpi --enable-icb --enable-eigen
make all install
tree "${TMP_PREFIX}"

# 3. Setup environment for PKG_CHECK_MODULES to work (what you typically in module-environment files).

cd "${TMP_DIR}"
export PKG_CONFIG_PATH="$(find ${TMP_PREFIX} -name arpack${LIBSUFFIX}${ITF64SUFFIX}.pc)"
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH%/*}" # Same as dirname.
echo "PKG_CONFIG_PATH: $PKG_CONFIG_PATH"

# 4. Create new autotools project, in temporary directory, with files from arpack-ng.

mkdir -p tstAutotoolsInstall
cd tstAutotoolsInstall

cp "${PROJECT_SOURCE_DIR}/EXAMPLES/BAND/dnband.f" .
cp "${PROJECT_SOURCE_DIR}/EXAMPLES/BAND/dnbdr1.f" .
cp "${PROJECT_SOURCE_DIR}/PARPACK/EXAMPLES/MPI/pdndrv1.f" .
cp -fr "${PROJECT_SOURCE_DIR}/m4" .
cp "${PROJECT_SOURCE_DIR}/TESTS/icb_arpack_c.c" .
cp "${PROJECT_SOURCE_DIR}/TESTS/icb_arpack_cpp.cpp" .
cp "${PROJECT_SOURCE_DIR}/PARPACK/TESTS/MPI/icb_parpack_c.c" .
cp "${PROJECT_SOURCE_DIR}/PARPACK/TESTS/MPI/icb_parpack_cpp.cpp" .
cp "${PROJECT_SOURCE_DIR}/EXAMPLES/MATRIX_MARKET/arpackmm.cpp" .

echo "AC_PREREQ([2.68])                                                       "  > configure.ac
echo "AC_INIT([tstAutotoolsInstall], 1.0)                                     " >> configure.ac
echo "AM_INIT_AUTOMAKE([foreign])                                             " >> configure.ac
echo "AC_CONFIG_MACRO_DIR([m4])                                               " >> configure.ac
echo "                                                                        " >> configure.ac
echo "AC_PROG_FC                                                              " >> configure.ac
echo "AC_PROG_CC                                                              " >> configure.ac
echo "AC_PROG_CXX                                                             " >> configure.ac
echo "AC_PROG_MKDIR_P                                                         " >> configure.ac
echo "AC_PROG_MAKE_SET                                                        " >> configure.ac
echo "PKG_PROG_PKG_CONFIG                                                     " >> configure.ac
echo "                                                                        " >> configure.ac
echo "AX_BLAS([], [AC_MSG_ERROR([cannot find BLAS libraries])])               " >> configure.ac
echo "AC_SUBST([BLAS_LIBS])                                                   " >> configure.ac
echo "                                                                        " >> configure.ac
echo "AX_LAPACK([], [AC_MSG_ERROR([cannot find LAPACK libraries])])           " >> configure.ac
echo "AC_SUBST([LAPACK_LIBS])                                                 " >> configure.ac
echo "                                                                        " >> configure.ac
echo "AC_LANG_PUSH([Fortran 77])                                              " >> configure.ac
echo "AX_MPI([], AC_MSG_ERROR([could not compile a Fortran MPI test program]))" >> configure.ac
echo "F77=\$MPIF77                                                            " >> configure.ac
echo "AC_SUBST([MPI_Fortran_LIBS])                                            " >> configure.ac
echo "                                                                        " >> configure.ac
echo "AC_LANG_PUSH([C])                                                       " >> configure.ac
echo "AX_MPI([], AC_MSG_ERROR([could not compile a C MPI test program]))      " >> configure.ac
echo "AC_SUBST([MPI_C_LIBS], ["\$MPILIBS \$FCLIBS"])                          " >> configure.ac
echo "CC=\$MPICC                                                              " >> configure.ac
echo "AC_LANG_POP([C])                                                        " >> configure.ac
echo "                                                                        " >> configure.ac
echo "AC_LANG_PUSH([C++])                                                     " >> configure.ac
echo "AX_MPI([], AC_MSG_ERROR([could not compile a C++ MPI test program]))    " >> configure.ac
echo "AC_SUBST([MPI_CXX_LIBS], ["\$MPILIBS \$FCLIBS"])                        " >> configure.ac
echo "CXX=\$MPICXX                                                            " >> configure.ac
echo "AC_LANG_POP([C++])                                                      " >> configure.ac
echo "                                                                        " >> configure.ac
echo "PKG_CHECK_MODULES([EIGEN3], [eigen3 >= 3.3])                            " >> configure.ac
echo "                                                                        " >> configure.ac
echo "PKG_CHECK_MODULES([ARPACK], [arpack${LIBSUFFIX}${ITF64SUFFIX}])         " >> configure.ac
echo "AC_SUBST([ARPACK_CFLAGS])                                               " >> configure.ac
echo "AC_SUBST([ARPACK_LIBS])                                                 " >> configure.ac
echo "                                                                        " >> configure.ac
echo "PKG_CHECK_MODULES([PARPACK], [parpack${LIBSUFFIX}${ITF64SUFFIX}])       " >> configure.ac
echo "AC_SUBST([PARPACK_CFLAGS])                                              " >> configure.ac
echo "AC_SUBST([PARPACK_LIBS])                                                " >> configure.ac
echo "                                                                        " >> configure.ac
echo "LT_INIT                                                                 " >> configure.ac
echo "                                                                        " >> configure.ac
echo "AC_CONFIG_FILES([Makefile])                                             " >> configure.ac
echo "AC_OUTPUT                                                               " >> configure.ac

echo "AUTOMAKE_OPTIONS = foreign                                                                           "  > Makefile.am
echo "ACLOCAL_AMFLAGS  = -I m4                                                                             " >> Makefile.am
echo "                                                                                                     " >> Makefile.am
echo "EXTRA_DIST = debug.h stat.h                                                                          " >> Makefile.am
echo "                                                                                                     " >> Makefile.am
echo "bin_PROGRAMS    = dnbdr1 pdndrv1 icb_arpack_c icb_arpack_cpp icb_parpack_c icb_parpack_cpp arpackmm  " >> Makefile.am
echo "                                                                                                     " >> Makefile.am
echo "dnbdr1_SOURCES = dnbdr1.f dnband.f                                                                   " >> Makefile.am
echo "dnbdr1_FFLAGS  = \$(ARPACK_CFLAGS)                                                                   " >> Makefile.am
echo "dnbdr1_LDADD   = \$(ARPACK_LIBS) \$(LAPACK_LIBS) \$(BLAS_LIBS)                                       " >> Makefile.am
echo "                                                                                                     " >> Makefile.am
echo "pdndrv1_SOURCES = pdndrv1.f dnband.f                                                                 " >> Makefile.am
echo "pdndrv1_FFLAGS  = \$(PARPACK_CFLAGS) \$(ARPACK_CFLAGS)                                               " >> Makefile.am
echo "pdndrv1_LDADD   = \$(PARPACK_LIBS) \$(ARPACK_LIBS) \$(LAPACK_LIBS) \$(BLAS_LIBS) \$(MPI_Fortran_LIBS)" >> Makefile.am
echo "                                                                                                     " >> Makefile.am
echo "icb_arpack_c_SOURCES = icb_arpack_c.c                                                                " >> Makefile.am
echo "icb_arpack_c_CPPFLAGS = \$(ARPACK_CFLAGS)                                                            " >> Makefile.am
echo "icb_arpack_c_LDADD = \$(ARPACK_LIBS) \$(LAPACK_LIBS) \$(BLAS_LIBS)                                   " >> Makefile.am
echo "                                                                                                     " >> Makefile.am
echo "icb_arpack_cpp_SOURCES = icb_arpack_cpp.cpp                                                          " >> Makefile.am
echo "icb_arpack_cpp_CPPFLAGS = \$(ARPACK_CFLAGS)                                                          " >> Makefile.am
echo "icb_arpack_cpp_LDADD = \$(ARPACK_LIBS) \$(LAPACK_LIBS) \$(BLAS_LIBS)                                 " >> Makefile.am
echo "                                                                                                     " >> Makefile.am
echo "arpackmm_SOURCES = arpackmm.cpp                                                                      " >> Makefile.am
echo "arpackmm_CPPFLAGS = \$(ARPACK_CFLAGS) \$(EIGEN3_CFLAGS)                                              " >> Makefile.am
echo "arpackmm_LDADD = \$(ARPACK_LIBS) \$(LAPACK_LIBS) \$(BLAS_LIBS)                                       " >> Makefile.am
echo "                                                                                                     " >> Makefile.am
echo "icb_parpack_c_SOURCES = icb_parpack_c.c                                                              " >> Makefile.am
echo "icb_parpack_c_CPPFLAGS = \$(AM_CPPFLAGS) \$(PARPACK_CFLAGS) \$(ARPACK_CFLAGS)                        " >> Makefile.am
echo "icb_parpack_c_LDADD = \$(PARPACK_LIBS) \$(ARPACK_LIBS) \$(LAPACK_LIBS) \$(BLAS_LIBS) \$(MPI_C_LIBS)  " >> Makefile.am
echo "                                                                                                        " >> Makefile.am
echo "icb_parpack_cpp_SOURCES = icb_parpack_cpp.cpp                                                           " >> Makefile.am
echo "icb_parpack_cpp_CPPFLAGS = \$(AM_CPPFLAGS) \$(PARPACK_CFLAGS) \$(ARPACK_CFLAGS)                         " >> Makefile.am
echo "icb_parpack_cpp_LDADD = \$(PARPACK_LIBS) \$(ARPACK_LIBS) \$(LAPACK_LIBS) \$(BLAS_LIBS) \$(MPI_CXX_LIBS) " >> Makefile.am

# 5. Build and test this new project with configure: for this to be possible, PKG_CHECK_MODULES must work.

autoreconf --force --verbose --install

./configure || exit 1
make all || exit 1

export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/tmp/tstAutotoolsInstall/local/lib"

./dnbdr1 || exit 1
./icb_arpack_c || exit 1
./icb_arpack_cpp || exit 1
./arpackmm -h || exit 1

mpirun -n 2 ./pdndrv1 || exit 1
mpirun -n 2 ./icb_parpack_c || exit 1
mpirun -n 2 ./icb_parpack_cpp || exit 1
